Note: This tutorial assumes that you have completed the previous tutorials: Error Handling. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Assertions in the Ecl
Description: how to utilise ecl's run-time and compile-time asserts/aborts.Keywords: ecl errors assertions
Tutorial Level: BEGINNER
Run-Time Assert/Abort
These functions, because they are coupled with fallback macros for debug/ndebug modes, are the only functions in the ecl not namespaced inside the ecl namespace. Rather they are prefixed with ecl_.
The ecl_run_time_assert macro/function is a tool for conditional testing. It is governed by the presence of the NDEBUG macro. If NDEBUG is absent (either in your code or c-flagged by the compiler (-DNDEBUG) then run_time_assert will equate to a macro that defers to a null function pointer. This is the fastest means of bypassing debugged code.
If NDEBUG is defined, then ecl_run_time_assert will act as a conditional test via a function. If the test fails, it will output some data (that you have passed to the function) before finally aborting.
The ecl_runtime_abort macro/function works similarly except that it does no conditional test and it is not negated by the presence of NDEBUG.
Compile-Time Assert
There is also a macro which works as a compile time equivalent of ecl_run_time_assert(). This macro is useful for checking template parameters mostly. When passed a logical condition which fails, an eye-catching COMPILE_TIME_FAILURE is reported in the compile log.
1 ecl_compile_time_assert( 1 > 3 )
Corresponding failure is reported in a format similar to the example output shown below:
1 test.cpp:47: error: invalid application of ‘sizeof’ to incomplete type ‘COMPILE_TIME_FAILURE<false>’